<?php
//废弃 可以删了
namespace app\api\controller;

use app\common\controller\Common;
use PhpOffice\PhpWord\PhpWord;
use think\Db;

class Sceenmodel extends Common
{
    public function list()
    {
        try {
            $param  = $this->param;
            $condition = !empty($param['condition']) ? $param['condition'] : '';
            $keyword = !empty($param['keyword']) ? $param['keyword'] : '';
            $page = !empty($param['page']) ? $param['page'] : '1';
            $limit = !empty($param['limit']) ? $param['limit'] : '10';
            $view_model = Model('SceenModel');
            $data = $view_model->getList($condition, $keyword, $page, $limit);
            return resultArray(['data' => $data]);
        } catch (\Exception $e) {
            return resultArray(['error' => $e->getMessage()]);
        }
    }

    public function detail()
    {
        $param  = $this->param;
        if (!$id = $param['id']) {
            return resultArray(['error' => '模板id为空']);
        }
        $detail_model = Model('SceenModel');
        $data = $detail_model->get($id);
        $data['config'] = json_decode($data->detail->config, true) ? json_decode($data->detail->config, true) : '';
        unset($data['detail']);
        return resultArray(['data' => $data]);
    }

    public function edit()
    {
        $param  = $this->param;
        if (!$id = $param['id']) {
            return resultArray(['error' => '模板id为空']);
        }
        $view_model = Model('SceenModel');
        $param['update_time'] = time();
        $result = $view_model->updateDataById($param, $param['id']);
        if (!$result) {
            return resultArray(['error' => $view_model->getError()]);
        }
        return resultArray(['data' => '']);
    }

    /**
     * 修改详情的配置项
     */
    public function config()
    {
        $param  = $this->param;
        if (!$id = $param['id']) {
            return resultArray(['error' => '模板id为空']);
        }
        unset($param['id']);
        $view_model = Model('SceenDetail');
        $result = $view_model->updateConfig($param, $id);
        if (!$result) {
            return resultArray(['error' => $view_model->getError()]);
        }
        return resultArray(['data' => '']);
    }

    /**
     * 修改默认项接口
     */
    public function defaultView()
    {
        $param  = $this->param;
        if (!$id = $param['id']) {
            return resultArray(['error' => '模板id为空']);
        }
        $view_model = Model('SceenModel');
        $result = $view_model->editDefaultModel($id);
        if (!$result) {
            return resultArray(['error' => $view_model->getError()]);
        }
        return resultArray(['data' => '']);
    }

    /**
     * 下载模板
     */
    public function downModel()
    {
        //phpword转格式
        $phpWord = new PhpWord();
        $template = config('huisheng.view_template_meeting');
        $document = $phpWord->loadTemplate($template);
        $param = json_decode($this->param['data'],true);
        $speech_model = Model('LiveSpeech');
        $lid = $speech_model->get(['meeting_tag' => $param['meeting_id']])->id;

        if(!$param['meeting_id'] || !$lid)
        {
            return '未找到下载文件！';
        }
        if(isset($param['manual']) && $param['manual'] == 1){
            $data = '';
            $sc = '';
            $cache_result = self::$redis->getMeetingResult($param['meeting_id']);
            ksort($cache_result);
            foreach($cache_result as $val){
                $value = json_decode($val, true);
                if($value['sc'] !=  $sc)
                {
                    $data .= "<w:br />\t";
                    $sc = $value['sc'];
                }
                $data .= $value['is_edit'] ? $value['ed_txt'] : $value['or_txt'];
            }
        }else{
            $data_arr = Db::name('meeting_convert')->where(['lid' => $lid])->order('sorting asc')->select();
            $data = '';
            $sc = '';
            foreach($data_arr as $value)
            {
                if($value['subsection'] !==  $sc)
                {
                    $data .= "<w:br />\t";
                    $sc = $value['subsection'];
                }
                $data .= $value['is_edit'] ? $value['editor'] : $value['transfer'];
            }
        }
        $param['data'] = substr($data,8);
        unset($param['meeting_id']);
        foreach($param as $key => $value)
        {
            $document->setValue($key, $value);
        }
        $docx_path = config('huisheng.word_result') . date('YmdHmi',time()) . ".docx";
        $document->saveAs($docx_path);
        $file = $docx_path;
        download_result_one($file);
    }
}
